package com.ourteam.product.model.api;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.frame.web.OperationResult;
import com.ourteam.product.model.dao.ProductModelDomainDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelDomainBean;
import com.ourteam.product.model.service.IProductModelService;
import com.ourteam.product.model.service.ProductModelServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/MODEL/productModelDomain")
public class ProductModelDomainAPI extends AbstractServiceController {

	private static final IProductModelService PRODUCT_MODEL_SERVICE = ProductModelServiceFactory
			.getProductModelService();

	@RequestMapping("/queryProductModelDomains")
	@ResponseBody
	public GridBean queryProductModelDomains(final @ModelAttribute ProductModelDomainSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductModelDomainDAOQueryBean, ProductModelDomainSearchModel> searchHelper = new AbstractSearchHelper<ProductModelDomainDAOQueryBean, ProductModelDomainSearchModel>(
				searchModel) {

			@Override
			protected ProductModelDomainDAOQueryBean createQueryBean() {
				return new ProductModelDomainDAOQueryBean();
			}

			@Override
			protected int queryCount(ProductModelDomainDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelDomainCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductModelDomainDAOQueryBean queryBean) throws Exception {
				return PRODUCT_MODEL_SERVICE.queryProductModelDomains(queryBean);
			}

		};

		return searchHelper.doSearch(request);

	}

	@RequestMapping("/deleteModelDomains")
	@ResponseBody
	public OperationResult deleteModelDomains(@RequestParam(name = "ids") long[] domainIds,
			HttpServletRequest request, Model model) throws Exception {

		PRODUCT_MODEL_SERVICE.deleteProductModelDomains(domainIds);

		return OperationResult.SUCCESS_RESULT;

	}

	@RequestMapping("/saveModelDomain")
	@ResponseBody
	public ProductModelDomainBean saveModelDomain(@ModelAttribute ProductModelDomainBean modelDomainBean,
			HttpServletRequest request, Model model) throws Exception {

		if (modelDomainBean.getId() != 0) {
			modelDomainBean.setLastModifierName(getLoginUserName());
			modelDomainBean.setLastModifyTime(new Date());
			PRODUCT_MODEL_SERVICE.modifyProductModelDomain(modelDomainBean);
		} else {
			modelDomainBean.setCreatorName(getLoginUserName());
			modelDomainBean.setCreateTime(new Date());
			PRODUCT_MODEL_SERVICE.addNewProductModelDomain(modelDomainBean);
		}

		return modelDomainBean;
	}

	@RequestMapping("/getModelDomainById")
	@ResponseBody
	public ProductModelDomainBean getModelDomainById(@RequestParam long id, HttpServletRequest request, Model model)
			throws Exception {

		return PRODUCT_MODEL_SERVICE.getProductModelDomainById(id);

	}

	@RequestMapping("/getModelDomains")
	@ResponseBody
	public ProductModelDomainBean[] getModelDomains() throws Exception {

		return PRODUCT_MODEL_SERVICE.queryProductModelDomains(new ProductModelDomainDAOQueryBean());

	}

}
